#!/usr/bin/env python
# coding=utf-8

import os
import sys

profile = "svnlist.txt"

if (len(sys.argv) >= 2):
    profile = sys.argv[1]
    print "Switch to profile " + profile

if (not os.path.exists(profile)):
    print "Profile " + profile + " not existed"
    exit(-1)

f = open(profile)

lists = f.readlines()
for line in lists:
    line = line.strip("\n")
    if (line.startswith("#")):
        print "skip : " + line
        continue

    tmp = line.split(" ")
    if (len(tmp) < 2):
    	print "error line : " + line
    	continue

    module_name = tmp[0]
    svn_path = tmp[1]
    svn_path = svn_path.strip("/")

    if (svn_path.find("/trunk/") >= 0): # trunk
        trunk = True
    else:
        trunk = False

    if (trunk):
        print "Note : " + "path is on the trunk"
        tmp = svn_path.split("/")
        svn_branch = "trunk"
        svn_module_name = tmp[-1]
        svn_module_base = "/".join(tmp[0:-1])
        svn_module_base = svn_module_base.replace("/trunk/", "/branches/");
    else:
        # get svn path split
        tmp = svn_path.split("/")
        svn_branch = tmp[-1]
        svn_module_name = tmp[-2]
        svn_module_base = "/".join(tmp[0:-2])

    # make ln -s for head
    src_dir = svn_module_base + "/" + module_name + "_" + svn_branch
    src_head_dir = svn_module_base + "/" + module_name
    os.system("rm " + src_head_dir)

    cmd = "ln -s " + module_name + "_" + svn_branch + " " + src_head_dir
    ret = os.system(cmd)
    if (ret != 0):
        print "[excute failed] " + cmd
        exit(-1)
    else:
        print "[excute successful] " + cmd